﻿namespace DCHealthCheck.MvcWeb.Controllers {

    #region Usings
    using DCHealthCheck.Common;
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes;
    using DCHealthCheck.MvcWeb.Classes.DataServices;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    #endregion

    public class ServicesController : BaseController {

        private readonly IServiceInfoDataService ServiceInfoDataService;

        public ServicesController(IServiceInfoDataService serviceInfoDataService) {
            if (serviceInfoDataService == null) {
                throw new ArgumentNullException("serviceInfoDataService");
            }
            this.ControllerName = "Services";
            this.ServiceInfoDataService = serviceInfoDataService;
        }

        [EnableCompression]
        [HttpPost]
        public FileContentResult Download(ServiceInfoViewModel viewModel, string token) {
            string fileName = string.Format("HealthCheckServices_{0}.csv", DateTime.UtcNow.ToString("yyyy-MM-dd-HHmmss"));
            string fileData = this.ServiceInfoDataService.GetCSVData(viewModel);

            HttpContext.Response.AppendCookie(new HttpCookie("fileDownloadToken", token));
            return File(fileData.ToByteArray(Encoding.UTF8), "application/octet-stream", fileName);
        }

        public ActionResult Index(ServiceInfoViewModel viewModel) {
            this.ServiceInfoDataService.ProcessData(viewModel);
            return View(viewModel);
        }
    }
}
